<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-message"></i> 消息通知</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-tabs v-model="message">
                <el-tab-pane :label="`未读消息(${unread.length})`" name="unRead">
                    <el-table :data="unread"  style="width: 100%">
                        <el-table-column label="消息内容">
                            <template slot-scope="scope">
                                <span class="message-title">{{scope.row.details}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="type" label="消息类型" width="200" align="center"></el-table-column>
                        <el-table-column label="来自" width="200" align="center">
                            <template slot-scope="scope">
                                {{scope.row.sendOne}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="createTime" label="时间" width="200" align="center"></el-table-column>
                        <el-table-column width="120">
                            <template slot-scope="scope">
                                <el-button size="small" @click="handleRead(scope.$index)">标为已读</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="handle-row">
                        <el-button type="primary" @click="allSetRead">全部标为已读</el-button>
                    </div>
                </el-tab-pane>
                <el-tab-pane :label="`已读消息(${read.length})`" name="read">
                    <template>
                        <el-table :data="read" style="width: 100%">
                            <el-table-column label="消息内容">
                                <template slot-scope="scope">
                                    <span class="message-title">{{scope.row.details}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="type" label="消息类型" width="200" align="center"></el-table-column>
                            <el-table-column label="来自" width="200" align="center">
                                <template slot-scope="scope">
                                    {{scope.row.sendOne}}
                                </template>
                            </el-table-column>
                            <el-table-column prop="createTime" label="时间" width="200" align="center"></el-table-column>
                            <el-table-column width="120">
                                <template slot-scope="scope">
                                    <el-button type="danger" @click="handleDel(scope.$index)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="handle-row">
                            <el-button type="danger" @click="allDelete">删除全部</el-button>
                        </div>
                    </template>
                </el-tab-pane>
                <el-tab-pane :label="`回收站(${recycle.length})`" name="recycle">
                    <template>
                        <el-table :data="recycle" style="width: 100%">
                            <el-table-column label="消息内容">
                                <template slot-scope="scope">
                                    <span class="message-title">{{scope.row.details}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="type" label="消息类型" width="200" align="center"></el-table-column>
                            <el-table-column label="来自" width="200" align="center">
                                <template slot-scope="scope">
                                    {{scope.row.sendOne}}
                                </template>
                            </el-table-column>
                            <el-table-column prop="createTime" label="时间" width="200" align="center"></el-table-column>
                            <el-table-column width="120">
                                <template slot-scope="scope">
                                    <el-button @click="handleRestore(scope.$index)">还原</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="handle-row">
                            <el-button type="danger" @click="deleteRecycle">清空回收站</el-button>
                        </div>
                    </template>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>

<script>
    import { getRequest, postRequest } from '../../api';
    import bus from '../common/bus';
    export default {
        name: 'messageInfo',
        data() {
            return {
                message: 'unRead',
                unread: [],
                read: [],
                recycle: []
            }
        },
        watch: {
            unread(){
                this.getMessageCount();
            }
        },
        methods: {
            getMessage(){
              getRequest('/getSystemMessageNotRead', {current: 1, size: 50}).then(res=>{
                  if(res){
                      this.unread=res.list;
                  }
              });
              getRequest('/getSystemMessageIsRead', {current: 1, size: 50}).then(res=>{
                  if(res){
                      this.read=res.list;
                  }
              });
                getRequest('/getSystemMessageIsDelete', {current: 1, size: 50}).then(res=>{
                    if(res){
                        this.recycle=res.list;
                    }
                })
            },
            handleRead(index) {
                const item = this.unread.splice(index, 1);
                this.read = item.concat(this.read);
                const params= {messageId: item[0].messageId};
                getRequest('/processRead', params).then(res=>{
                    if(res){
                        this.$message.success('消息已设为已读！');
                    }
                })
            },
            handleDel(index) {
                const item = this.read.splice(index, 1);
                this.recycle = item.concat(this.recycle);
                const params= {messageId: item[0].messageId, isDelete: 1};
                getRequest('/processDelete', params).then(res=>{
                    if(res){
                        this.$message.success('消息已放入回收站！');
                    }
                })
            },
            handleRestore(index) {
                const item = this.recycle.splice(index, 1);
                this.read = item.concat(this.read);
                const params= {messageId: item[0].messageId, isDelete: 0};
                getRequest('/processDelete', params).then(res=>{
                    if(res){
                        this.$message.success('消息已还原！');
                    }
                })
            },
            allSetRead(){
                let messageIdList=[];
                this.unread.forEach(item=>{
                    messageIdList.push(item.messageId);
                });
                if(messageIdList.length===0){
                    return false;
                }
                postRequest('/multipartProcessRead', {messageIdList: messageIdList} ).then(res=>{
                    if(res==='success'){
                        this.$message.success('消息已全部设置为已读！');
                        this.read=this.unread.concat(this.read);
                        this.unread=[];
                    }
                })
            },
            allDelete(){
                let messageIdList=[];
                this.read.forEach(item=>{
                    messageIdList.push(item.messageId);
                });
                if(messageIdList.length===0){
                    return false;
                }
                postRequest('/multipartProcessDelete', {messageIdList: messageIdList} ).then(res=>{
                    if(res==='success'){
                        this.$message.success('消息已清空！');
                        this.recycle=this.read.concat(this.recycle);
                        this.read=[];
                    }
                })
            },
            deleteRecycle(){
                this.$confirm('确定要永久删除这些消息吗？删除后将不可恢复！','警告', {type: 'error'}).then(()=>{
                    let messageIdList=[];
                    this.recycle.forEach(item=>{
                        messageIdList.push(item.messageId);
                    });
                    if(messageIdList.length===0){
                        return false;
                    }
                    postRequest('/deleteRecycle', {messageIdList: messageIdList} ).then(res=>{
                        if(res==='success'){
                            this.$message.success('消息已永久删除！');
                            this.recycle=[];
                        }
                    })
                }).catch(()=>{
                })
            },
            getMessageCount(){
                bus.$emit('messageCount', this.unread.length);
            }
        },
        computed: {

        },
        mounted() {
            this.getMessage();
        }
    }

</script>

<style>
.message-title{
    cursor: pointer;
}
.handle-row{
    margin-top: 30px;
}
</style>

